<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.security.spec.RSAPublicKeySpec"%>
<%@page import="java.security.PrivateKey"%>
<%@page import="java.security.PublicKey"%>
<%@page import="java.security.KeyFactory"%>
<%@page import="java.security.KeyPair"%>
<%@page import="java.security.KeyPairGenerator"%>
<%
int KEY_SIZE = 1024;
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(KEY_SIZE);

KeyPair keyPair = generator.genKeyPair();
KeyFactory keyFactory = KeyFactory.getInstance("RSA");

PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();

session = request.getSession();
// 세션에 공개키의 문자열을 키로하여 개인키를 저장한다.
session.setAttribute("rsaPrivateKey", privateKey);

// 공개키를 문자열로 변환하여 JavaScript RSA 라이브러리 넘겨준다.
RSAPublicKeySpec publicSpec = (RSAPublicKeySpec) keyFactory.getKeySpec(publicKey, RSAPublicKeySpec.class);

String publicKeyModulus = publicSpec.getModulus().toString(16);
String publicKeyExponent = publicSpec.getPublicExponent().toString(16);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Expires" content="-1"/> 
<meta http-equiv="Pragma" content="no-cache"/> 
<meta http-equiv="Cache-Control" content="no-cache"/> 

<script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="/js/rsa/jsbn.js"></script>
<script type="text/javascript" src="/js/rsa/rsa.js"></script>
<script type="text/javascript" src="/js/rsa/prng4.js"></script>
<script type="text/javascript" src="/js/rsa/rng.js"></script>
<script type="text/javascript">

function f_submit(){
    var frm = document.getElementById("frm");
    var passwd = frm.passwd.value;
    
    var rsaPublicKeyModulus = frm.publicKeyModulus.value;
    var rsaPublicKeyExponent = frm.publicKeyExponent.value;

    var rsa = new RSAKey();
    rsa.setPublic(rsaPublicKeyModulus, rsaPublicKeyExponent);

    var securedPassword = rsa.encrypt(passwd);

    frm.passwd.value = securedPassword;
    frm.submit();
}

function f_keyCheck(){
	if(event.keyCode == '13'){
		f_submit();
		event.returnValue = false; 
		event.cancelBubble = true;
	}
}

$(document).ready(function(){
	
	$("#btnSubmit").click(f_submit);
	
	$("#passwd").keypress(f_keyCheck);
	
});


</script>
<link href="/css/style.css" rel="stylesheet" type="text/css">
<title>here i am..</title>
</head>
<body>
	come on baby~<br />
	<form id="frm" name="frm" action="list.do" method="post">
        <input type="hidden" name="publicKeyModulus" id="publicKeyModulus" value="<%=publicKeyModulus%>" />
        <input type="hidden" name="publicKeyExponent" id="publicKeyExponent" value="<%=publicKeyExponent%>" />
		<input type="password" id="passwd" name="passwd" value="" />
		<input type="button" id="btnSubmit" value="Run" />
	</form>

</body>
</html>